//
//  HomeTableViewController.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/13.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    //表格绑定的微博数据数组
    var statusesList : [Status]? {
        
        didSet{
            //刷新表格
            tableView.reloadData()
        }
        
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        //设置 home 界面的信息
        vistorView?.setupViewInfo("visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜", isHome: true)
        
        setNavigationBar()
        loadData()
        
        //注册 可重用的 Cell  一定要注册  不注册的话会崩溃
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: "Cell")
        
        
    }
    
    //加载微博数据
    private func loadData() {
        
        Status.loadStatus { (statuses) -> () in
            //把从网络上获取到的微博数组 传递给statusesList
            self.statusesList = statuses
        }
    }
    
    
    //MARK : - TableView 的数据源方法
    //返回多少行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusesList?.count ?? 0
        
    }
    
    // 返回的 Cell 长什么样
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //此方法必须注册可重用的Cell 如果不注册会崩溃
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! StatusCell
        
        //设置 Cell
        cell.status = statusesList![indexPath.row]
        
        return cell
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //开启动画
        vistorView?.startAnimation()
    }
    
    //初始化一个转场动画对象
    let popAnimator = PopoverAnimator()
    
    //中心按钮的点击事件
    func titleBtnClick() {
        // 获取转场的控制器和 ViewController
        let sb = UIStoryboard(name: "FriendGroup", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("FriendGroupSB")
        
        
        //1.设置转场'transation'的代理
        vc.transitioningDelegate = popAnimator
        //2.设置视图的展现大小
        let x = (view.bounds.width - 200) * 0.5
        popAnimator.presentFrame = CGRectMake(x, 56, 200, 240)
        //3.设置转场模式
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        
        //显示转场控制器
        presentViewController(vc, animated: true , completion: nil)
        
        
    }
    
    
    //导航啦右侧按钮的点击事件
    func showQRCode() {
        
        //获取二维码sb 
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        
        presentViewController(sb.instantiateInitialViewController()!, animated: true, completion: nil)
        
    }
    
    
    
    //自定义导航栏
    func setNavigationBar() {
        
        if sharedUserAccount == nil {
            return
        }
        
        //设置左按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", highlightImageName: nil)
        
        //设置右按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", highlightImageName: nil)
        
        //1.1获取 button
        let btn = navigationItem.rightBarButtonItem?.customView as! UIButton
        
        //1.2添加点击事件
        btn.addTarget(self, action: "showQRCode", forControlEvents: UIControlEvents.TouchUpInside)
    
        
        //设置中间标题
        //取出名字
        let titleBtn = HomeTitleBtn.button(sharedUserAccount!.name!)
  
        titleBtn.addTarget(self, action: "titleBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleBtn
    }
    
   
    
    
}









